﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 系统角色仓储
/// </summary>
public class SysRoleRep : BaseRep<SysRoleModel>
{
    /// <summary>
    /// 字段
    /// </summary>
    private static string sqlFields = string.Format(@"
         `role_id`
        ,`role_name`
        ,`role_key`
        ,`role_sort`
        ,`data_scope`
        ,`menu_check_strictly`
        ,`dept_check_strictly`
        ,`status`
        ,`del_flag`
        ,`create_by`
        ,`create_time`
        ,`update_by`
        ,`update_time`
        ,`remark`
    ");

    /// <summary>
    /// 通过角色编号获取系统角色信息
    /// </summary>
    /// <param name="roleId">角色编号</param>
    /// <returns>SysRoleModel</returns>
    public SysRoleModel? GetSysRoleByRoleId(long roleId)
    {
        string sqlWhere = " and `role_id` = @RoleId ";

        return QuerySingle(sqlFields, sqlWhere, new { RoleId = roleId });
    }
    /// <summary>
    /// 获取系统角色列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>IEnumerable<SysRoleModel></returns>
    public IEnumerable<SysRoleModel> GetSysRoleList(SysRoleModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.RoleName))
        {
            sqlWhere += " and `role_name` like concat('%', @RoleName, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.RoleKey))
        {
            sqlWhere += " and `role_key` like concat('%', @RoleKey, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.DataScope))
        {
            sqlWhere += " and `data_scope` = @DataScope ";
        }
        if (model.MenuCheckStrictly > 0)
        {
            sqlWhere += " and `menu_check_strictly` = @MenuCheckStrictly ";
        }
        if (model.DeptCheckStrictly > 0)
        {
            sqlWhere += " and `dept_check_strictly` = @DeptCheckStrictly ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }
    /// <summary>
    /// 分页获取系统角色列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<SysRoleModel></returns>
    public Page<SysRoleModel> GetSysRolePageList(SysRoleModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.RoleName))
        {
            sqlWhere += " and `role_name` like concat('%', @RoleName, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.RoleKey))
        {
            sqlWhere += " and `role_key` like concat('%', @RoleKey, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.DataScope))
        {
            sqlWhere += " and `data_scope` = @DataScope ";
        }
        if (model.MenuCheckStrictly > 0)
        {
            sqlWhere += " and `menu_check_strictly` = @MenuCheckStrictly ";
        }
        if (model.DeptCheckStrictly > 0)
        {
            sqlWhere += " and `dept_check_strictly` = @DeptCheckStrictly ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }
    /// <summary>
    /// 通过角色编号更新系统角色
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateSysRoleByRoleId(SysRoleModel model)
    {
        var sqlSet = string.Empty ;

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.RoleName))
        {
            sqlSet += " `role_name` = @RoleName, ";
        }

        if (!string.IsNullOrWhiteSpace(model.RoleKey))
        {
            sqlSet += " `role_key` = @RoleKey, ";
        }

        if (model.RoleSort > -1)
        {
            sqlSet += " `role_sort` = @RoleSort, ";
        }

        if (!string.IsNullOrWhiteSpace(model.DataScope))
        {
            sqlSet += " `data_scope` = @DataScope, ";
        }

        if (model.MenuCheckStrictly > -1)
        {
            sqlSet += " `menu_check_strictly` = @MenuCheckStrictly, ";
        }

        if (model.DeptCheckStrictly > -1)
        {
            sqlSet += " `dept_check_strictly` = @DeptCheckStrictly, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        if (model.Remark != null)
        {
            sqlSet += " `remark` = @Remark, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `role_id` = @RoleId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过系统角色编号删除系统角色
    /// </summary>
    /// <param name="roleId">系统角色编号</param>
    /// <returns>int</returns>
    public int DeleteSysRoleByRoleId(long roleId)
    {
        string sqlWhere = " and `role_id` = @RoleId ";

        return Delete(sqlWhere, new { RoleId = roleId });
    }
}

